package com.hp.gxw.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.constant.R;
import com.hp.gxw.entity.CostProcessEquipment;
import com.hp.gxw.entity.CostProductData;
import com.hp.gxw.service.ICostProcessEquipmentService;
import com.hp.gxw.service.ICostProductDataService;
import com.hp.gxw.utils.DateUtil;
import com.hp.gxw.utils.HttpRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.YearMonth;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-02-21
 */
@Controller
@RequestMapping("/manage/product_data")
public class CostProductDataController {

    @Autowired
    private ICostProductDataService iCostProductDataService;

    @Autowired
    private ICostProcessEquipmentService iCostProcessEquipmentService;

    @Autowired
    private HttpRequestUtils httpRequestUtils;

    @RequestMapping("/equipmentHtml")
    public String equipmentHtml(String workOrder, String processCode, Model model){
        model.addAttribute("workOrder", workOrder);
        model.addAttribute("processCode", processCode);
        return "cost_product_data/equipment_detail";
    }

    @RequestMapping("/pull")
    @ResponseBody
    public R pull(@RequestBody String month){
        if(StringUtils.isEmpty(month)){
            return R.error("参数异常");
        }
        try {
            //删除已存在的数据
            iCostProductDataService.remove(new LambdaQueryWrapper<CostProductData>()
                    .between(CostProductData::getEndTime, DateUtil.getFirstDayOfMonth(month), DateUtil.getLastDayOfMonth(month)));
            iCostProcessEquipmentService.remove(new LambdaQueryWrapper<CostProcessEquipment>().eq(CostProcessEquipment::getMonth, month));
            YearMonth yearMonth = YearMonth.of(Integer.valueOf(month.split("-")[0]), Integer.valueOf(month.split("-")[1]));
            int daysInMonth = yearMonth.lengthOfMonth();
            for (int day = 1; day <= daysInMonth; day++) {
                List<CostProductData> list = httpRequestUtils.requestProductData(month + "-" + (day < 10 ? "0" + day : day));
                if(list != null && list.size() > 0){
                    iCostProductDataService.saveBatch(list);
                    for (CostProductData costProductData : list) {
                        if(costProductData.getProcessEquipmentList().size() > 0){
                            iCostProcessEquipmentService.saveBatch(costProductData.getProcessEquipmentList());
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error("系统异常");
        }
        return R.ok();
    }

    @PostMapping("/equipment_detail")
    @ResponseBody
    public LayTableR equipment_detail(String workOrder, String processCode, Integer page, Integer limit){
        List<CostProcessEquipment> list = iCostProcessEquipmentService.list(new LambdaQueryWrapper<CostProcessEquipment>()
                .eq(CostProcessEquipment::getWorkOrder, workOrder)
                .eq(CostProcessEquipment::getProcessCode, processCode));
        return LayTableR.result(0, list.size(), list);
    }
}

